﻿namespace MarqueeLabel
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class Label : UserControl
    {
        private Color _color = Color.White;
        private int _distance = 10;
        private System.Windows.Forms.Orientation _orientation = System.Windows.Forms.Orientation.Horizontal;
        private string _text = "";
        private IContainer components;
        private System.Windows.Forms.Label lblFirst;
        private System.Windows.Forms.Label lblSecond;
        private Panel panelBack;
        private Timer tmrActive;
        private bool OnlyOnce = false;
        int AlphaValue = 100;
        public Label()
        {
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.tmrActive = new System.Windows.Forms.Timer(this.components);
            this.lblFirst = new System.Windows.Forms.Label();
            this.lblSecond = new System.Windows.Forms.Label();
            this.panelBack = new System.Windows.Forms.Panel();
            this.panelBack.SuspendLayout();
            this.SuspendLayout();
            // 
            // tmrActive
            // 
            this.tmrActive.Interval = 50;
            this.tmrActive.Tick += new System.EventHandler(this.tmrActive_Tick);
            // 
            // lblFirst
            // 
            this.lblFirst.AutoSize = true;
            this.lblFirst.BackColor = System.Drawing.Color.Transparent;
            this.lblFirst.Font = new System.Drawing.Font("宋体", 13.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblFirst.ForeColor = System.Drawing.SystemColors.ControlText;
            this.lblFirst.Location = new System.Drawing.Point(8, 8);
            this.lblFirst.Name = "lblFirst";
            this.lblFirst.Size = new System.Drawing.Size(0, 24);
            this.lblFirst.TabIndex = 0;
            this.lblFirst.Paint += new System.Windows.Forms.PaintEventHandler(this.lblFirst_Paint);
            // 
            // lblSecond
            // 
            this.lblSecond.AutoSize = true;
            this.lblSecond.BackColor = System.Drawing.Color.Transparent;
            this.lblSecond.Location = new System.Drawing.Point(8, 24);
            this.lblSecond.Name = "lblSecond";
            this.lblSecond.Size = new System.Drawing.Size(0, 15);
            this.lblSecond.TabIndex = 1;
            // 
            // panelBack
            // 
            this.panelBack.BackColor = System.Drawing.Color.Transparent;
            this.panelBack.Controls.Add(this.lblSecond);
            this.panelBack.Controls.Add(this.lblFirst);
            this.panelBack.Location = new System.Drawing.Point(128, 0);
            this.panelBack.Name = "panelBack";
            this.panelBack.Size = new System.Drawing.Size(200, 100);
            this.panelBack.TabIndex = 2;
            // 
            // Label
            // 
            this.BackColor = System.Drawing.Color.Transparent;
            this.Controls.Add(this.panelBack);
            this.Name = "Label";
            this.Size = new System.Drawing.Size(112, 16);
            this.Load += new System.EventHandler(this.Label_Load);
            this.FontChanged += new System.EventHandler(this.Label_FontChanged);
            this.Resize += new System.EventHandler(this.Label_Resize);
            this.ForeColorChanged += new System.EventHandler(this.Label_ForeColorChanged);
            this.EnabledChanged += new System.EventHandler(this.Label_EnabledChanged);
            this.BindingContextChanged += new System.EventHandler(this.Label_BindingContextChanged);
            this.SizeChanged += new System.EventHandler(this.Label_SizeChanged);
            this.panelBack.ResumeLayout(false);
            this.panelBack.PerformLayout();
            this.ResumeLayout(false);

        }


        private void Label_BindingContextChanged(object sender, EventArgs e)
        {
        }

        private void Label_EnabledChanged(object sender, EventArgs e)
        {
            this.lblFirst.Enabled = this.lblSecond.Enabled = this.tmrActive.Enabled = base.Enabled;
        }

        private void Label_FontChanged(object sender, EventArgs e)
        {
            this.lblFirst.Font = this.lblSecond.Font = this.Font;
            this.lblSecond.Top = base.Height;
            this.lblFirst.Top = this.lblFirst.Left = this.lblSecond.Left = 0;
        }

        private void Label_ForeColorChanged(object sender, EventArgs e)
        {
            this.lblFirst.ForeColor = this.lblSecond.ForeColor = this.ForeColor;
        }

        private void Label_Load(object sender, EventArgs e)
        {
            this.panelBack.Left = this.panelBack.Top = 0;
            this.panelBack.Height = base.Height;
            this.panelBack.Width = base.Width;
            this.panelBack.Left = this.panelBack.Top = 0;
            this.panelBack.Height = base.Height;
            this.panelBack.Width = base.Width;
            this.panelBack.Left = this.panelBack.Top = 0;
            if (this._orientation == System.Windows.Forms.Orientation.Vertical)
            {
                if (this.lblFirst.Height > this.panelBack.Height)
                {
                    this.lblSecond.Top = this.lblFirst.Height;
                }
                else
                {
                    this.lblSecond.Top = this.panelBack.Height;
                }
                this.lblFirst.Top = this.lblFirst.Left = this.lblSecond.Left = 0;
            }
            else
            {
                this.lblFirst.Top = this.lblSecond.Top = 0;
                lblFirst.Left = panelBack.Width + 1;
            }
            SetStyle(ControlStyles.SupportsTransparentBackColor
          | ControlStyles.UserPaint
          | ControlStyles.AllPaintingInWmPaint
          | ControlStyles.Opaque, true);
            BackColor = Color.Transparent;
        }

        private void Label_Resize(object sender, EventArgs e)
        {
            this.panelBack.Left = this.panelBack.Top = 0;
            this.panelBack.Height = base.Height;
            this.panelBack.Width = base.Width;
            this.panelBack.Left = this.panelBack.Top = 0;
            if (this._orientation == System.Windows.Forms.Orientation.Vertical)
            {
                if (this.lblFirst.Height > this.panelBack.Height)
                {
                    this.lblSecond.Top = this.lblFirst.Height;
                }
                else
                {
                    this.lblSecond.Top = this.panelBack.Height;
                }
                this.lblFirst.Top = this.lblFirst.Left = this.lblSecond.Left = 0;
            }
            else
            {
                this.lblFirst.Left = this.lblFirst.Top = this.lblSecond.Top = 0;
                if (this.lblFirst.Width > this.panelBack.Width)
                {
                    this.lblSecond.Left = this.lblFirst.Width;
                }
                else
                {
                    this.lblSecond.Left = this.panelBack.Width;
                }
            }
        }

        private void Label_SizeChanged(object sender, EventArgs e)
        {
            this.panelBack.Left = this.panelBack.Top = 0;
            this.panelBack.Height = base.Height;
            this.panelBack.Width = base.Width;
            this.panelBack.Left = this.panelBack.Top = 0;
            if (this._orientation == System.Windows.Forms.Orientation.Vertical)
            {
                if (this.lblFirst.Height > this.panelBack.Height)
                {
                    this.lblSecond.Top = this.lblFirst.Height;
                }
                else
                {
                    this.lblSecond.Top = this.panelBack.Height;
                }
                this.lblFirst.Top = this.lblFirst.Left = this.lblSecond.Left = 0;
            }
            else
            {
                this.lblFirst.Left = this.lblFirst.Top = this.lblSecond.Top = 0;
                if (this.lblFirst.Width > this.panelBack.Width)
                {
                    this.lblSecond.Left = this.lblFirst.Width;
                }
                else
                {
                    this.lblSecond.Left = this.panelBack.Width;
                }
            }
        }

        private void lblFirst_Paint(object sender, PaintEventArgs e)
        {
        }

        private void tmrActive_Tick(object sender, EventArgs e)
        {
            if (this._orientation == System.Windows.Forms.Orientation.Horizontal)
            {
                this.lblFirst.Left -= this._distance;
                if (!OnlyOnce)
                {
                    this.lblSecond.Left -= this._distance;
                }
                if ((this.lblFirst.Left + this.lblFirst.Width) <= 0)
                {
                    tmrActive.Enabled = false;
                }
                if (!OnlyOnce)
                {
                    if ((this.lblSecond.Left + this.lblSecond.Width) <= 0)
                    {
                        if (this.lblFirst.Width >= this.panelBack.Width)
                        {
                            this.lblSecond.Left = this.lblFirst.Width + this.lblFirst.Left;
                        }
                        else
                        {
                            this.lblSecond.Left = this.lblFirst.Left + this.panelBack.Width;
                        }
                    }
                }
            }
            else
            {
                this.lblFirst.Top -= this._distance;
                if (!OnlyOnce)
                {
                    this.lblSecond.Top -= this._distance;
                }
                if ((this.lblFirst.Top + this.lblFirst.Height) <= 0)
                {
                    if (this.lblSecond.Height >= this.panelBack.Height)
                    {
                        this.lblFirst.Top = (this.lblSecond.Height + this.lblSecond.Top) - 1;
                    }
                    else
                    {
                        this.lblFirst.Top = (this.lblSecond.Top + this.panelBack.Height) - 1;
                    }
                }
                if (!OnlyOnce)
                {
                    if ((this.lblSecond.Top + this.lblSecond.Height) <= 0)
                    {
                        if (this.lblFirst.Height >= this.panelBack.Height)
                        {
                            this.lblSecond.Top = (this.lblFirst.Height + this.lblFirst.Top) - 1;
                        }
                        else
                        {
                            this.lblSecond.Top = (this.lblFirst.Top + this.panelBack.Height) - 1;
                        }
                    }
                }
            }
        }

        [Category("Appearance")]
        public System.Windows.Forms.BorderStyle BorderStyle
        {
            get
            {
                return this.panelBack.BorderStyle;
            }
            set
            {
                this.panelBack.BorderStyle = value;
            }
        }

        /// <summary>
        /// 循环
        /// </summary>
        public bool Once
        {
            get
            {
                return OnlyOnce;
            }
            set
            {
                OnlyOnce = value;
            }
        }

        /// <summary>
        /// 透明
        /// </summary>
        public int Transparency
        {
            get
            {
                return AlphaValue;
            }
            set
            {
                //AlphaValue = value;
                //BackColor = Color.FromArgb(AlphaValue, BackColor);
                BackColor = Color.Transparent;
            }
        }

        [Category("Appearance")]
        public string Caption
        {
            get
            {
                return this._text;
            }
            set
            {
                this._text = value;
                this.lblFirst.AutoSize = this.lblSecond.AutoSize = true;
                this.lblFirst.Text = this.lblSecond.Text = this._text;
                int width = this.lblFirst.Width;
                this.lblFirst.AutoSize = this.lblSecond.AutoSize = false;
                this.lblFirst.Width = this.lblSecond.Width = width;
                string[] strArray = this._text.Split("\n".ToCharArray(), 0x10000);
                this.lblFirst.Height = this.lblSecond.Height = this.lblFirst.Font.Height * strArray.Length;
                if (this._orientation == System.Windows.Forms.Orientation.Horizontal)
                {
                    if (this.lblFirst.Left > this.lblSecond.Left)
                    {
                        if (this.lblSecond.Width >= this.panelBack.Width)
                        {
                            this.lblFirst.Left = this.lblSecond.Width + this.lblSecond.Left;
                        }
                        else
                        {
                            this.lblFirst.Left = this.lblSecond.Left + this.panelBack.Width;
                        }
                    }
                    else if (this.lblFirst.Width >= this.panelBack.Width)
                    {
                        this.lblSecond.Left = this.lblFirst.Width + this.lblFirst.Left;
                    }
                    else
                    {
                        this.lblSecond.Left = this.lblFirst.Left + this.panelBack.Width;
                    }
                }
                else if (this.lblFirst.Top > this.lblSecond.Top)
                {
                    if (this.lblSecond.Height >= this.panelBack.Height)
                    {
                        this.lblFirst.Top = this.lblSecond.Height + this.lblSecond.Top;
                    }
                    else
                    {
                        this.lblFirst.Top = this.lblSecond.Top + this.panelBack.Height;
                    }
                }
                else if (this.lblFirst.Height >= this.panelBack.Height)
                {
                    this.lblSecond.Top = this.lblFirst.Height + this.lblFirst.Top;
                }
                else
                {
                    this.lblSecond.Top = this.lblFirst.Top + this.panelBack.Height;
                }
            }
        }

        [Category("Behavior")]
        public int Distance
        {
            get
            {
                return this._distance;
            }
            set
            {
                if (value <= 0)
                {
                    this._distance = 3;
                }
                else
                {
                    this._distance = value;
                }
            }
        }

        [Category("Behavior")]
        public bool EnableMarquee
        {
            get
            {
                return this.tmrActive.Enabled;
            }
            set
            {
                this.tmrActive.Enabled = value;
                if (!this.tmrActive.Enabled)
                {
                    this.panelBack.Left = this.panelBack.Top = 0;
                    this.panelBack.Height = base.Height;
                    this.panelBack.Width = base.Width;
                    this.panelBack.Left = this.panelBack.Top = 0;
                    if (this._orientation == System.Windows.Forms.Orientation.Vertical)
                    {
                        if (this.lblFirst.Height > this.panelBack.Height)
                        {
                            this.lblSecond.Top = this.lblFirst.Height;
                        }
                        else
                        {
                            this.lblSecond.Top = this.panelBack.Height;
                        }
                        this.lblFirst.Top = this.lblFirst.Left = this.lblSecond.Left = 0;
                    }
                    else
                    {
                        this.lblFirst.Left = this.lblFirst.Top = this.lblSecond.Top = 0;
                        if (this.lblFirst.Width > this.panelBack.Width)
                        {
                            this.lblSecond.Left = this.lblFirst.Width;
                        }
                        else
                        {
                            this.lblSecond.Left = this.panelBack.Width;
                        }
                    }
                }
            }
        }

        [Browsable(false)]
        public System.Drawing.Font Font
        {
            get
            {
                return this.lblFirst.Font;
            }
        }

        [Category("Behavior")]
        public System.Windows.Forms.Orientation Orientation
        {
            get
            {
                return this._orientation;
            }
            set
            {
                this._orientation = value;
                if (this._orientation == System.Windows.Forms.Orientation.Vertical)
                {
                    if (this.lblFirst.Height > this.panelBack.Height)
                    {
                        this.lblSecond.Top = this.lblFirst.Height;
                    }
                    else
                    {
                        this.lblSecond.Top = this.panelBack.Height;
                    }
                    this.lblFirst.Top = this.lblFirst.Left = this.lblSecond.Left = 0;
                }
                else
                {
                    this.lblFirst.Left = this.lblFirst.Top = this.lblSecond.Top = 0;
                    if (this.lblFirst.Width > this.panelBack.Width)
                    {
                        this.lblSecond.Left = this.lblFirst.Width;
                    }
                    else
                    {
                        this.lblSecond.Left = this.panelBack.Width;
                    }
                }
            }
        }

        [Category("Behavior")]
        public int Speed
        {
            get
            {
                return this.tmrActive.Interval;
            }
            set
            {
                this.tmrActive.Interval = value;
            }
        }

        [Category("Appearance")]
        public System.Drawing.Font TextFont
        {
            get
            {
                return this.lblFirst.Font;
            }
            set
            {
                this.lblFirst.AutoSize = this.lblSecond.AutoSize = true;
                this.lblFirst.Font = this.lblSecond.Font = value;
                int width = this.lblFirst.Width;
                this.lblFirst.AutoSize = this.lblSecond.AutoSize = false;
                this.lblFirst.Width = this.lblSecond.Width = width;
                this.lblFirst.AutoSize = this.lblSecond.AutoSize = false;
                string[] strArray = this.lblFirst.Text.Split("\n".ToCharArray(), 0x10000);
                this.lblFirst.Height = this.lblSecond.Height = this.lblFirst.Font.Height * strArray.Length;
                if (this._orientation == System.Windows.Forms.Orientation.Horizontal)
                {
                    if (this.lblFirst.Left > this.lblSecond.Left)
                    {
                        if (this.lblSecond.Width >= this.panelBack.Width)
                        {
                            this.lblFirst.Left = this.lblSecond.Width + this.lblSecond.Left;
                        }
                        else
                        {
                            this.lblFirst.Left = this.lblSecond.Left + this.panelBack.Width;
                        }
                    }
                    else if (this.lblFirst.Width >= this.panelBack.Width)
                    {
                        this.lblSecond.Left = this.lblFirst.Width + this.lblFirst.Left;
                    }
                    else
                    {
                        this.lblSecond.Left = this.lblFirst.Left + this.panelBack.Width;
                    }
                }
                else if (this.lblFirst.Top > this.lblSecond.Top)
                {
                    if (this.lblSecond.Height >= this.panelBack.Height)
                    {
                        this.lblFirst.Top = this.lblSecond.Height + this.lblSecond.Top;
                    }
                    else
                    {
                        this.lblFirst.Top = this.lblSecond.Top + this.panelBack.Height;
                    }
                }
                else if (this.lblFirst.Height >= this.panelBack.Height)
                {
                    this.lblSecond.Top = this.lblFirst.Height + this.lblFirst.Top;
                }
                else
                {
                    this.lblSecond.Top = this.lblFirst.Top + this.panelBack.Height;
                }
            }
        }
    }
}

